package org.web.ctrlcv.login.controller;

import java.util.HashMap;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class LoginController {
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Locale locale, Model model, HttpServletRequest request) throws Exception {
		return "login";
	}
	
	@RequestMapping(value = "/login/check", method = RequestMethod.POST)
    public void loginCheck(Locale locale, Model model, @RequestParam HashMap<String, String> paramMap,
    		HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		String user_name = paramMap.get("USER_EMAIL").substring(0,paramMap.get("USER_EMAIL").indexOf("@"));
		session.setAttribute("USER_NAME", user_name);
		JSONObject object = new JSONObject();
		object.put("RTN_CD", "0");
		object.put("RTN_MSG", "ok");
		logger.debug("<!> object : "+object.toString());
		model.addAttribute("data", object.toString());
		response.getWriter().write(object.toString());
		//return "redirect:/main/login.do";
	}
	
	@RequestMapping(value = "/join", method = RequestMethod.GET)
    public String join(Locale locale, Model model, HttpServletRequest request) throws Exception {
		return "join";
	}
}